package com.wangcang.widgetlibrary.tools;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.wangcang.widgetlibrary.activity.constant.Constant;
import com.wangcang.widgetlibrary.bean.BankBean;
import com.wangcang.widgetlibrary.bean.CommonClass;
import com.wangcang.widgetlibrary.bean.PayBean;
import com.wangcang.widgetlibrary.bean.WeChatBean;
import com.wangcang.widgetlibrary.http.NoHttpUtils;

import java.util.HashMap;

/**
 * Description: 支付工具类
 */

public class PayUtil extends PayBaseUtil implements NoHttpUtils.OnResponseListener {

    //  银联后台环境标识，“00”将在银联正式环境发起交易,“01”将在银联测试环境发起交易
    private final       String mMode                = Constant.UNOINPAYMODE;
    private Activity mContext;
    private ProgressDialog mDialog;

    public PayUtil(Activity mContext) {
        super(mContext);
        this.mContext = mContext;
    }

    @Override
    public void onSucceed(CommonClass commonClass, int what) {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
        switch (what) {
            case 0:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    try {
                        parseData(commonClass.getData().toString());
                    } catch (Exception e) {
                        ToastUtils.showToast(e.getMessage());
                        e.printStackTrace();
                    }
                } else {
                    Log.i("Error", commonClass.getMsg());
                }
                break;
            case 1:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    WeChatBean weChatBean = JSON.parseObject(commonClass.getData().toString(),WeChatBean.class);
                        sendPayRequest(weChatBean);

//                    //公司付款
//                    if (!commonClass.getData().getRegisterType().isEmpty()) {
//                        WXOrderInforGS wxOrderInforGS = JSON.parseObject(response.get(), WXOrderInforGS.class);
//                        WXprepayId = wxOrderInforGS.getData().getPrepayid();
//                        WXregisterType = wxOrderInforGS.getData().getRegisterType();
//                        Intent intent = new Intent(WXcontext, WXPayEntryActivity.class);
//                        intent.putExtra("registerType", "GS");
//                        WXcontext.startActivity(intent);
//                    } else {
//                        //个人付款
//                        WXprepayId = wxOrderInfor.getData().getPrepayid();
//                        WXregisterType = wxOrderInfor.getData().getRegisterType();
//                        sendPayRequest(wxOrderInfor);
//                    }
                }else{
                }
                break;
            case 2:
                try {
                    if ("0".equals(commonClass.getCode())) {
                        BankBean bankBean = JSON.parseObject(commonClass.getData().toString(),BankBean.class);
                        String mTn = bankBean.getTn();
                        if (mTn == null || (mTn).length() == 0) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                            builder.setTitle("错误提示");
                            builder.setMessage("网络连接失败,请重试!");
                            builder.setNegativeButton("确定",
                                    new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                        }
                                    });
                            builder.create().show();
                        } else {
                            doStartUnionPayPlugin(mContext, mTn, mMode);
                            /*************************************************
                             * 步骤2：通过银联工具类启动支付插件
                             ************************************************/
                        }

                    }

                } catch (Exception e) {
                    if (mDialog.isShowing()) {
                        mDialog.dismiss();
                    }
                    ToastUtils.showToast(e.toString());
                }
                break;
        }
    }

    @Override
    public void onFailed(String result, int what) {
        mDialog.dismiss();
    }

    @Override
    public void onFinish(int what) {
        mDialog.dismiss();
    }

    /**
     * 支付宝支付
     */
    public void aliPay(PayBean payBean) {
        mDialog = new ProgressDialog(mContext);
        mDialog.show();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("amount", payBean.getAmount());
        hashMap.put("clientIp", payBean.getClientIp());
        hashMap.put("openId", payBean.getOpenId());
        hashMap.put("p2c", payBean.isP2c());
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("regAppType", payBean.getRegAppType());
        hashMap.put("title", payBean.getTitle());
        hashMap.put("tradeNo", payBean.getTradeNo());
//        hashMap.put("method", "acPay.financialPay");
//        hashMap.put("w_appid", "iphone");
//        hashMap.put("t", "1111");
//        hashMap.put("version", "v1.0");
        NoHttpUtils.httpPost(Constant.ALIPAY, hashMap, this, 0);

    }

    /**
     * 微信支付
     */
    public void WXpay(PayBean payBean) {
        mDialog = new ProgressDialog(mContext);
        mDialog.show();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("amount", payBean.getAmount());
        hashMap.put("clientIp", payBean.getClientIp());
        hashMap.put("openId", payBean.getOpenId());
        hashMap.put("p2c", payBean.isP2c());
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("regAppType", payBean.getRegAppType());
        hashMap.put("title", payBean.getTitle());
        hashMap.put("tradeNo", payBean.getTradeNo());
//        hashMap.put("method", "acPay.financialPay");
//        hashMap.put("w_appid", "iphone");
//        hashMap.put("t", "1111");
//        hashMap.put("version", "v1.0");
        NoHttpUtils.httpPost(Constant.ALIPAY, hashMap, this, 1);
    }

    /**
     * 银联支付，部分回调需要在Activity  onActivityResult中进行
     */
    public void unionPay(PayBean payBean) {
        mDialog = new ProgressDialog(mContext);
        mDialog.show();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("amount", payBean.getAmount());
        hashMap.put("clientIp", payBean.getClientIp());
        hashMap.put("openId", payBean.getOpenId());
        hashMap.put("p2c", payBean.isP2c());
        hashMap.put("platFormId", payBean.getPlatFormId());
        hashMap.put("regAppType", payBean.getRegAppType());
        hashMap.put("title", payBean.getTitle());
        hashMap.put("tradeNo", payBean.getTradeNo());
//        hashMap.put("method", "acPay.financialPay");
//        hashMap.put("w_appid", "iphone");
//        hashMap.put("t", "1111");
//        hashMap.put("version", "v1.0");
        NoHttpUtils.httpPost(Constant.ALIPAY, hashMap, this, 2);
    }

}